package com.guyu.common.core.enums;


import com.guyu.common.core.enums.able.IEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;

/**
 * 布尔值
 */
@Getter
@AllArgsConstructor
public enum BooleanEnum implements IEnum<BooleanEnum> {

    NO(0, false, "N", "否"),
    YES(1, true, "Y", "是");

    private final Integer code;
    private final boolean value;
    private final String type;
    private final String desc;

    /**
     * code!=1都认为是false
     */
    public static BooleanEnum getEnum(String type) {
        if (type == null || type.isEmpty()) {
            return null;
        }
        for (BooleanEnum yesOrNoEnum : values()) {
            if (Objects.equals(yesOrNoEnum.getType(), type)) {
                return yesOrNoEnum;
            }
        }
        return null;
    }

    /**
     * code!=1都认为是false
     */
    public static boolean isYes(Integer code) {
        return Objects.equals(BooleanEnum.YES.getCode(), code);
    }

    public static BooleanEnum getEnum(boolean value) {
        return value ? BooleanEnum.YES : BooleanEnum.NO;
    }

    public static Integer getCode(boolean value) {
        return value ? BooleanEnum.YES.code : BooleanEnum.NO.code;
    }

    @Override
    public BooleanEnum getDefault() {
        return NO;
    }

}
